package assets

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shimmer/neusoft_club_backend/controllers/utils"
	"shimmer/neusoft_club_backend/dao/domain"
	"shimmer/neusoft_club_backend/middlewares/auth_middleware"
	"shimmer/neusoft_club_backend/services"
	"shimmer/neusoft_club_backend/services/assets_service"
	"shimmer/neusoft_club_backend/services/club_service"
	"strconv"
)

// @Summary 上传头像
// @Tags 资源
// @Description 使用form提交上传!!!
// @Param file body interface{} true "要上传的文件"
// @Success 200 {object} domain.AssetsDTO
// @Router /api/assets/user/avatar [post]
func uploadAvatar(c *gin.Context) {
	user, err := domain.GetUserFromContext(c)
	file, err := c.FormFile("file")
	if err != nil {
		services.AbortWithError(c, err)
		return
	}

	res, err := assets_service.SaveUploadAsset(file, c, &assets_service.SaveUploadAssetMutation{
		OwnerID: &user.ID,
	})

	if err != nil {
		services.AbortWithError(c, err)
		return
	}
	user.AvatarID = &res.ID
	if err = services.DBEngine.Save(user).Error; err != nil {
		services.AbortWithError(c, err)
		return
	}
	c.JSON(http.StatusOK, res)
	return
}

// @Summary 上传资源
// @Tags 资源
// @Description 使用form提交上传!!!
// @Param file body interface{} true "要上传的文件"
// @Success 200 {object} domain.AssetsDTO
// @Router /api/assets/ [post]
func uploadAssets(c *gin.Context) {
	var ownerId *int64 = nil
	user, _ := domain.GetUserFromContext(c)
	file, err := c.FormFile("file")
	if err != nil {
		services.AbortWithError(c, err)
		return
	}
	if user != nil {
		ownerId = &user.ID
	}
	res, err := assets_service.SaveUploadAsset(file, c, &assets_service.SaveUploadAssetMutation{
		OwnerID: ownerId,
	})
	if err != nil {
		services.AbortWithError(c, err)
		return
	}
	c.JSON(http.StatusOK, res)
	return
}

// @Summary 随机抽取资源
// @Tags 资源
// @Description
// @Param count query integer false "抽取的数量"
// @Success 200 {object} []domain.AssetsDTO
// @Router /api/assets/shuffle [get]
func shuffleAssets(c *gin.Context) {
	count, _ := strconv.Atoi(c.Query("count"))
	if count == 0 {
		count = 10
	}
	if count > 100 {
		count = 100
	}
	assets, err := assets_service.ShuffleAssets(count)
	if err != nil {
		services.AbortWithError(c, err)
		return
	}

	c.JSON(http.StatusOK, assets)
}
// @Summary 获取资源列表
// @Tags 资源
// @Description
// @Param page query integer false "页面"
// @Param size query integer true "页面大小"
// @Success 200 {object} []domain.AssetsDTO
// @Router /api/assets [get]
func listAssets(c *gin.Context) {
	user,err := domain.GetUserFromContext(c)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	isAdmin,err := club_service.CheckIsAdmin(user.ID)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	pageQuery, err := utils.GetPaginationFromContext(c)
	if err != nil {
		services.AbortWithError(c,err)
		return
	}
	where := map[string]interface{}{}
	if !isAdmin {
		where["owner_id"] = user.ID
	}
	res,err := assets_service.FindAssets(where,pageQuery)
	if err != nil {
		services.AbortWithError(c, err)
		return
	}
	c.JSON(http.StatusOK,res)
}
func ConfigureAssetsController(r *gin.RouterGroup) {
	r.GET("/",auth_middleware.AuthLoginMiddleware(),listAssets)
	r.POST("/", uploadAssets)
	r.POST("/user/avatar", auth_middleware.AuthLoginMiddleware(), uploadAvatar)
	r.GET("/shuffle", shuffleAssets)

}
